{{define "title"}}Builder {{ .Builder.Name }}{{end}}

{{define "blamelist"}}
{{ if . }}
  {{ $nb := len . }}
  {{ if lt $nb 4 }}
    {{ range $i, $c := . -}}
      {{- if gt $i 0 }}, {{ end -}}
      {{- if .Changelist -}}
        {{- .Changelist.HTML -}}
        {{- if .AuthorEmail -}}
          by {{- .AuthorEmail -}}
        {{- end -}}
      {{- else if .AuthorEmail -}}
        (<a href="{{ .CommitURL }}">{{ .AuthorEmail }}</a>)
      {{- else -}}
        (<a href="{{ .CommitURL }}">Unknown Author</a>)
      {{- end -}}
    {{- end -}}
  {{- else -}}
    ({{ $nb }} changes)
  {{- end }}
{{ end }}
{{end}}

{{define "body"}}

{{ $hasBlamelist := .Builder.HasBlamelist }}

<h1>Builder <a href="{{ pagedURL .Request -1 "EMPTY" }}">{{.Builder.Name}}</a></h1>

<div class="prelude">
  <div class="column">
  {{ if .Builder.MachinePool }}
    {{ with .Builder.MachinePool }}
      <h2>
      {{ if .SwarmingHost }}
        <a href="{{ .SwarmingURL }}">Machine Pool</a>
      {{ else }}
        Machine Pool
      {{ end }}
      </h2>
      <table>
        <tr>
          <td>Idle</td><td>{{ .Idle }}</td>
          <td class="graph">
            {{ if gt .Idle 0 }}
              <div class="bar idle" style="width: {{ percent .Idle .Total }}%"></div>
            {{ end }}
          </td>
        </tr><tr>
          <td>Busy</td><td>{{ .Busy }}</td>
          <td class="graph">
            {{ if gt .Busy 0 }}
              <div class="bar busy" style="width: {{ percent .Busy .Total }}%"></div>
            {{ end }}
          </td>
        </tr><tr>
          <td>Offline</td><td>{{ .Offline }}</td>
          <td class="graph">
            {{ if gt .Offline 0 }}
              <div class="bar offline"
                   style="width: {{ percent .Offline .Total }}%"></div>
            {{ end }}
          </td>
        </tr>
      </table>
      <details>
        <summary>{{ .Total }} bots</summary>
        {{ if eq (len .Bots) 1000 }}
          Note: This list may be incomplete, click on the Machine Pool link above to see all bots.
        {{ end }}
        <table class="info">
          <tr><th>Name</th><th>Status</th><th>Last Seen</th></tr>
          {{ range .Bots }}
            <tr>
              <td>{{ .Label.HTML }}</td>
              <td class="status-{{ .Status.String }}">{{ .Status.String }}</td>
              <td>{{ .LastSeen | localTime "N/A" }}</td>
            </tr>
          {{ end }}
        </table>
      </details>
    {{ end }}
  {{ end }}
  </div>

  <div class="column">
  {{ if .Builder.CurrentBuilds }}
    <h2>Current Builds ({{len .Builder.CurrentBuilds}}):</h2>
    <ul>
    {{ range .Builder.CurrentBuilds }}
      <li>{{ .Link.HTML }}
        {{ with .Blame }}{{ template "blamelist" }}{{ end }}
        [Running for: {{ .ExecutionTime.Duration | humanDuration }}]
        {{ range .Text }} {{ . }}{{ end }}
      </li>
    {{ end }}
    </ul>
  {{ else }}
    <h2>No current builds</h2>
  {{ end }}
  </div>

  <div class="column">
  {{ if .Builder.PendingBuilds -}}
		<h2>Pending Build Requests (
			{{- if gt .Builder.PendingBuildNum (len .Builder.PendingBuilds) -}}
				{{ .Builder.PendingBuildNum }} showing
			{{ end -}}{{- len .Builder.PendingBuilds -}}):</h2>
    <ul>
    {{- range .Builder.PendingBuilds -}}
      <li>
        {{- .Link.HTML }}
        <small>({{- .PendingTime.Started | localTime "" }}, waiting {{ .PendingTime.Duration | humanDuration -}})</small>
        {{- with .Blame }}{{- template "blamelist" -}}{{- end -}}
      </li>
    {{- end -}}
    </ul>
  {{ else }}
    <h2>No Pending Build Requests</h2>
  {{ end }}
  </div>

  {{- if .Builder.Groups -}}
  <div class="column">
    <h2>Builder groups</h2>
    <ul>
    {{- range .Builder.Groups -}}
      <li>{{ .HTML }}</li>
    {{- end -}}
    </ul>
  </div>
  {{- end }}
</div>

<div class="clear"></div>

<div class="main">
  {{ if .Builder.FinishedBuilds }}
    <h2>Recent Builds:</h2>

    <table class="info">
      <tr>
        <th>Finished Time</th>
        <th>Run Duration</th>
        <th>Revision</th>
        <th>Result</th>
        <th>Build #</th>{{ if $hasBlamelist }}
        <th>Changes</th>
        {{ end }}<th>Info</th>
      </tr>

      {{ range .Builder.FinishedBuilds }}
        <tr>
          <td>{{ .ExecutionTime.Finished | localTime "" }}</td>
          <td>{{ .ExecutionTime.Duration | humanDuration }}</td>
          <td class="revision">
            {{ .Revision.RevisionHTML }}
          </td>
          {{ if eq .Status.String "Success" }}
            <td class="status-Success">Success</td>
          {{ else if eq .Status.String "Failure" }}
            <td class="status-Failure">Failed</td>
          {{ else if eq .Status.String "InfraFailure" }}
            <td class="status-Exception">Infra Failure</td>
          {{ else if eq .Status.String "Exception" }}
            <td class="status-Exception">Exception</td>
          {{ else }}
            <td> {{ .Status.String }} </td>
          {{ end }}
          <td>{{ .Link.HTML }}</td>
          {{ if $hasBlamelist }}
            <td class="left">
              {{ $nb := len .Blame }}
              {{ if lt $nb 4 }}
                {{ range $i, $c := .Blame }}
                  {{ if gt $i 0 }},{{ end }}
                  {{ if .Changelist }}
                    {{ .Changelist.HTML }}
                    {{ if .AuthorEmail }}
                      by {{ .AuthorEmail }}
                    {{ end }}
                  {{ else }}
                    (<a href="{{ .CommitURL }}">{{ .AuthorEmail }}</a>)
                  {{ end }}
                {{ end }}
              {{ else }}
                ({{ $nb }} changes)
              {{ end }}
            </td>
          {{ end }}
          <td class="left">
            {{ range $i, $value := .Text }}
              {{ if gt $i 0 }}<br>{{ end }}
              {{ . }}
            {{ end }}
          </td>
        </tr>
      {{ end }}
    </table>
  {{ else }}
    <h2>No Recent Builds</h2>
  {{ end }}
</div>

<div class="epiloge">
  {{ if .Builder.PrevCursor }}<a href="{{ pagedURL .Request 0 .Builder.PrevCursor }}">Prev</a>{{ end }}
  (Show:
  <a href="{{ pagedURL .Request -1 "" }}">default</a>
  <a href="{{ pagedURL .Request 25 "" }}">25</a>
  <a href="{{ pagedURL .Request 50 "" }}">50</a>
  <a href="{{ pagedURL .Request 100 "" }}">100</a>
  <a href="{{ pagedURL .Request 200 "" }}">200</a>
  )
  {{ if .Builder.NextCursor }}
    <a href="{{ pagedURL .Request 0 .Builder.NextCursor}}">Next</a>
  {{ end }}
</div>

{{end}}
